/*
 * @Author: jiu yin
 * @Date: 2023-04-27 14:23:19
 * @LastEditTime: 2025-02-06 15:43:53
 * @LastEditTime: 2024-10-10 02:00:08
 * @LastEditors: jiu yin zhen jing
 * @FilePath: \web-admin\vite.config.ts
 * jiu
 */
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import path from "path";
import UnoCSS from "unocss/vite";
import { codeInspectorPlugin } from "code-inspector-plugin";
import { visualizer } from "rollup-plugin-visualizer";
import viteCompression from "vite-plugin-compression";
import importToCDN from "vite-plugin-cdn-import";
import ip from "ip"

export default defineConfig(configEnv => {
   return {
      css: {
         preprocessorOptions: {
            scss: {
               silenceDeprecations: ["legacy-js-api"], //清除你个sb警告
               api: "modern-compiler" // or 'modern' 使用最新api
            }
         }
      },

      plugins: [
         react(),
         UnoCSS(),
         codeInspectorPlugin({
            bundler: "vite"
         }),
         // viteCompression({
         //    verbose: true, // 是否在控制台中输出压缩结果
         //    disable: false, // 是否禁用压缩
         //    threshold: 10240, // 压缩阈值，超过该值才会进行压缩
         //    algorithm: "gzip", // 压缩算法，这里使用gzip
         //    ext: ".gz", // 压缩后的文件扩展名
         //    deleteOriginFile: true //删除源文件
         // }),

         importToCDN({
            enableInDevMode: true, //开发环境是否启用
            modules: [
               {
                  name: "axios",
                  var: "axios",
                  path: "https://resource.ym2meta.com/public/axios.min.js"
               },
               {
                  name: "echarts",
                  var: "echarts",
                  path: "https://resource.ym2meta.com/public/echarts.min.js"
               },
               {
                  name: "html2canvas",
                  var: "html2canvas",
                  path: "https://resource.ym2meta.com/public/html2canvas.min.js"
               }
               // {
               //    name: "dayjs",
               //    var: "dayjs",
               //    path: "https://cdn.jsdelivr.net/npm/dayjs@1.11.12/dayjs.min.js"
               // },
               // {
               //    name: "react",
               //    var: "React",
               //    path: `https://cdn.jsdelivr.net/npm/react@18.2.0/umd/react.production.min.js`

               // },
               // {
               //    name: "react-dom",
               //    var: "ReactDOM",
               //    alias: ["react-dom/client"],
               //    path: `https://cdn.jsdelivr.net/npm/react-dom@18.2.0/umd/react-dom.production.min.js`
               // },

               // {
               //    name: "antd",
               //    var: "antd",
               //    path: "https://cdn.jsdelivr.net/npm/antd@5.20.1/dist/antd.min.js"
               // },
               // {
               //    name: "@ant-design/icons",
               //    var: "@ant-design/icons",
               //    path: `https://cdnjs.cloudflare.com/ajax/libs/ant-design-icons/5.5.1/index.umd.js`,
               // }
            ]
         })
      ],
      resolve: {
         alias: {
            "@": path.resolve(__dirname, "./src")
         }
         // extensions 数组的意思是在import组件的时候自动补全.vue等文件后缀
         // extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
      },
      server: {
         open: true,
         port: 9000,
         host: ip.address(),
      },

      optimizeDeps: {
         // 预构建依赖
         include: ["react", "react-dom", "axios", "react-router-dom", "sass", "lodash", "gsap", "antd"],
         // 排除不需要预构建的依赖
         exclude: []
      },

      build: {
         minify: "esbuild",
         terserOptions: {
            parallel: true,  // 开启多线程压缩
            compress: {
               //生产环境时移除console
               drop_console: true,
               drop_debugger: true
            }
         },
         cache: true,
         cacheDir: "node_modules/.vite", //隐藏缓存的文件
         cssCodeSplit: true, // 开启 CSS 代码分割
         rollupOptions: {
            plugins: [
               visualizer({
                  open: true, //依赖包可视化
                  filename: "./sizeAnalyze.html", // 生成的分析报告文件名
                  gzipSize: true, // 收集 gzip 大小并显示
                  brotliSize: true // 收集 brotli 大小并显示
               })
            ],
            output: {
               manualChunks(id) {
                  if (id.includes("node_modules") && (id.endsWith(".js") || id.endsWith(".ts"))) {
                     return "vendor";
                  }
               }
            },

            cache: true // 启用 Rollup 缓存
         }
      }
   };
});
